package linuxbox.status.bean;

import java.util.*;

import linuxbox.BoxStatic;
import linuxbox.exception.*;
import linuxbox.bean.*;
import linuxbox.util.*;
import linuxbox.status.logic.ShortMsgObject;

public class ShortMsgBean extends ShortMsgObject implements ITableBean {
	protected DbAdapter m_db = null;
	protected static String m_TableName = "shortmsg";
  
	public ShortMsgBean(ShortMsgKey pk, DbAdapter db) {
		this.init(pk, db);
	}

	public ShortMsgBean(ShortMsgKey pk) {
		this.init(pk, null);
	}

	public ShortMsgBean() {
		this.init(null, null);
	}

	public void init(ShortMsgKey pk, DbAdapter db) {
		if (db != null) {
			m_db = db;
		}
		
		if (pk != null)
			m_iID = pk.getID();
	}

	public String getTableName() {
		return m_TableName;
	}
	
	public void setTableName(String str) {
		m_TableName = str;
	}

	public void makeConnection() throws Exception {
		if ( m_db == null )
			m_db = new DbAdapter(true);
	}
	
	public void closeConnection() throws Exception {
		if (m_db != null)
			m_db.close();
	}

	public void load() throws Exception {
		String sql;

		sql = "select id, from, to, sendtime, subject, message from "
		    + m_TableName + " where id = " + m_iID;
    
	    makeConnection();
		m_db.executeQuery(sql);

		int i = 1;
		if (m_db.next()) {
			m_iID = m_db.getInt(i++);
			m_sFrom = m_db.getString(i++);
			m_sTo = m_db.getString(i++);
			m_tsSendTime = m_db.getTimestamp(i++);
			m_sSubject = m_db.getString(i++);
			m_sMessage = m_db.getString(i++);
		} else {
			closeConnection();
			throw new LinuxboxException(ExceptionHelper.STATUS, "U000002");
		}
		
		sql = "select u.firstname, u.lastname from shortmsg s, usercontrol u "
		    + " where s.from = u.loginid and s.id = " + m_iID;
		m_db.executeQuery(sql);
		i = 1;
		if (m_db.next()) {
			String first = m_db.getString(i++);
			String last = m_db.getString(i++);
            m_sFromName = BoxStatic.formater.formatName(last, first);
		} else {
			closeConnection();
			throw new LinuxboxException(ExceptionHelper.STATUS, "U000002");
		}

		sql = "select u.firstname, u.lastname from shortmsg s, usercontrol u "
		    + " where s.to = u.loginid and s.id = " + m_iID;
		m_db.executeQuery(sql);
		i = 1;
		if (m_db.next()) {
			String first = m_db.getString(i++);
			String last = m_db.getString(i++);
            m_sToName = BoxStatic.formater.formatName(last, first);
		} else {
			closeConnection();
			throw new LinuxboxException(ExceptionHelper.STATUS, "U000002");
		}

		closeConnection();
	}

	public void store() throws Exception {
		String time = BoxStatic.formater.postgresTimestamp(m_tsSendTime);
		        
		String sql = "UPDATE " + m_TableName + " set"
			+ " id = " + m_iID + ","
		    + " from = '" + m_sFrom + "',"
		    + " to = '" + m_sTo + "',"
		    + " sendtime = '" + time + "',"
		    + " subject = '" + m_sSubject + "',"
		    + " message = '" + m_sMessage + "'"
		    + " where id = " + m_iID;
		             
		makeConnection();
		m_db.executeUpdate(sql);
		closeConnection();
	}

	public void refresh() throws Exception {
		load();
	}

	public void create() throws Exception {
	
	}

	public void create(ShortMsgObject obj) throws Exception {
		String time = BoxStatic.formater.postgresTimestamp(obj.getSendTime());

		String sql = null;
		makeConnection();
		
		// id must have value, cause id in Status table should be the 
		// same as id in UserControl table.
		int id = obj.getID();
		
		sql = "insert into " + m_TableName + "(id, from, to, sendtime, "
			+ "subject, message) values(" 
			+ id + ", '" + obj.getFrom() + "', '"
			+ obj.getTo() + ", '"
			+ time + "', '"
			+ obj.getSubject() + "', '"
			+ obj.getMessage() + "')";

		BoxStatic.logger.debug(sql);
		m_db.executeUpdate(sql);

		closeConnection();
	}

	public void remove() throws Exception {
		String sql = "delete from " + m_TableName + " where id = " + m_iID;
		makeConnection();
		m_db.executeUpdate(sql);
		closeConnection();
	}

	public Object findByKey() throws Exception {
		String sql = "select id from " + m_TableName + " where id = " + m_iID;
		makeConnection();
		m_db.executeQuery(sql);
		ShortMsgKey pk = null;

		if(m_db.next()) {
			pk = new ShortMsgKey(m_db.getInt(1));
		}
		closeConnection();
		return pk;
	}

	public Iterator findBySql(String sqlWhere) throws LinuxboxException {
		List v = new ArrayList();

		try {
			String sql = "select id from " + m_TableName + sqlWhere;
			BoxStatic.logger.debug(sql);
			makeConnection();
			m_db.executeQuery(sql);
			ShortMsgKey pk = null;

			while (m_db.next()) {
				pk = new ShortMsgKey(m_db.getInt(1));
				v.add(pk);
			}
			closeConnection();
		} catch(Exception e) {
			throw new LinuxboxException(ExceptionHelper.STATUS, "U000002", 
				"Can't find record: " + e.getMessage());
		}
		return v.iterator();
	}
}
